package controller;

import com.github.pagehelper.PageInfo;
import model.DemoPojo;
import model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import result.Result;
import service.PersonService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by walter on 2016/3/29.
 */
@Controller
public class PersonController {
    @Resource
    private PersonService personService;

    @RequestMapping(value = "login.do" )
    @ResponseBody
    public Result add(String name,String gender,Integer age){
        System.out.println(name);
        Person person = new Person();
        person.setName(name);
        person.setAge(age);
        person.setDate(new Date());
        person.setGender(gender);
        personService.add(person);
        Result result = new Result();
        result.setMessage("添加完成");
        result.setCode(1);
        result.setPerson(person);
        return result;
    }
    @RequestMapping(value = "find.do" )
    @ResponseBody
    public Result findOnePerson(Integer id){
        Person person = personService.findOne(id);
        Result result = new Result();
        if(null == person){
            result.setMessage("没有该会员");
            result.setPerson(person);
            result.setCode(0);
        }else {
            result.setMessage("找到了");
            result.setPerson(person);
            result.setCode(1);
        }
        return result;
    }
    @RequestMapping(value = "page.do" )
    @ResponseBody
    public Result page(Integer pageNum,Integer pageSize){
        Integer start = (pageNum-1)*pageSize;
       List<Person>list = personService.page(start,pageSize);
        Result result = new Result();
        result.setMessage("添加完成");
        result.setCode(1);
        result.setList(list);
        return result;
    }

    @RequestMapping(value = "page1.do" )
    @ResponseBody
    public Result pageByPlugin(Integer pageNum,Integer pageSize){
        PageInfo<Person> pageInfo = personService.pageByPlugins(pageNum,pageSize);
        Result result = new Result();
        result.setMessage("添加完成");
        result.setCode(1);
        result.setPageInfo(pageInfo);
        return result;
    }

    @RequestMapping(value = "page2.do" )
    @ResponseBody
    public Result all(Integer pageNum,Integer pageSize){
        PageInfo<DemoPojo> pageInfo = personService.all(pageNum,pageSize);
        Result result = new Result();
        result.setMessage("添加完成");
        result.setCode(1);
        result.setObj(pageInfo);
        return result;
    }

    @RequestMapping(value = "add.do" )
    @ResponseBody
    public Result add(Person person){
        System.out.println(person.getName());
        personService.add(person);
        Result result = new Result();
        result.setMessage("添加完成");
        result.setCode(1);
        result.setPerson(person);
        return result;
    }
}
